/*-----------------------------------------------File Info------------------------------------------------
** File Name:               AIP5916.c  
** Last modified date:      20222620
** Last version:            V0.1
** Description:             
**--------------------------------------------------------------------------------------------------------            
** Created date:            20220620
** author:                  YLY
** Version:                 V0.1
** Descriptions:            键盘扫描处理函数
**--------------------------------------------------------------------------------------------------------
*/
/*************************************************************
* 	include 
* 	头文件	
*************************************************************/
#include "I2cWR.h"
#include "key.h"
#include "AIP5916.h"

const tsAipCfg gtAipCfg[] = AIP_CFG;

/*******************************************************************************
*名 称: aip1926Init()
*功 能: aip1926Init 功能初始化
*入口参数: 无
*出口参数: 无
*说 明://AIP59xx必须放第一位初始化(随便什么I2C地址都有ACK)
*******************************************************************************/
bool aip59xxInit(void)
{
	uint8_t buf[4];
	uint8_t i;
	uint8_t ret = 0;
	
	do
	{
		buf[0] = 0x5A;
		ret = i2cWriteReg(AIP5916_ADDR, SYSCONADDR, buf,1);		// 0X5A:进入配置模式		0X00：进入工作模式
		if(ret == FAIL)
		{
			ret	= 1;
			break;
		}	
		bspDelayMs(2);
		
		buf[0] = 0;
		ret = i2cReadReg(AIP5916_ADDR, SYSCONADDR, buf, 1);
		if(ret == FAIL)
		{
			ret	= 2;
			break;
		}
		if(buf[0] != 0x5A)
		{
			ret	= 3;
			break;
		}		
		
			
		buf[0] = 0X5A;
		ret = i2cWriteReg(AIP5916_ADDR, SYSCONADDR, buf, 1);	// 0X5A:进入配置模式		0X00：进入工作模式
		if(ret == FAIL)
		{
			ret	= 4;
			break;
		}	
		
	#if(typefg==0x5908)
		buf[0] = 0x54;
		ret = i2cWriteReg(AIP5916_ADDR, MCONADDR,buf, 1);    	//芯片工作频率：1MHz	  多键模式  bit6和BIT4必须为1
		if(ret == FAIL)
		{
			ret	= 5;
			break;
		}	
	#else
		buf[0] = 0X04;
		ret = i2cWriteReg(AIP5916_ADDR, MCONADDR, buf, 1);		// 芯片工作频率：1MHz		bit2=0：单键模式/多键模式	/低电平有效;  //设置工作频率500KHZ
		if(ret == FAIL)
		{
			ret	= 6;
			break;	
		}
	#endif
		
		buf[0] = 0x20;
		ret = i2cWriteReg(AIP5916_ADDR, BURADDR, buf, 1);			//默认0X20 0X00:基线更新速度加快
		if(ret == FAIL)
		{
			ret	= 7;
			break;	
		}
		
		buf[0] = 	0x00;//0x00;
		ret = i2cWriteReg(AIP5916_ADDR, DMR0ADDR, buf, 1);			// 开发模式寄存器
		if(ret == FAIL)
		{
			ret	= 8;
			break;
		}
			
		buf[0] = 	0x10;//0x10;	
		ret = i2cWriteReg(AIP5916_ADDR, DMR1ADDR, buf, 1);			//量产模式：0X00,0X10; 开发模式：0X08 0XFF
		if(ret == FAIL)
		{
			ret	= 9;
			break;	
		}
			
		buf[0] = 0X00;
		ret = i2cWriteReg(AIP5916_ADDR, KDR0ADDR, buf, 1);			// 0：K00-K07：使能触摸按键		1：禁止触摸通道
			if(ret == FAIL)
		{
			ret	= 10;
			break;
		}
		
	#if(typefg==0x5908)
		
	#else
		buf[0] = 0XF0;	//0XFF	//10~14
		ret = i2cWriteReg(AIP5916_ADDR, KDR1ADDR, buf, 1); 			// 0：K10-K17：使能触摸按键		1：禁止触摸通道
		if(ret == FAIL)
		{
			ret	= 11;
			break;
		}
			
		buf[0] = 0XFF;
		ret = i2cWriteReg(AIP5916_ADDR, K0CONADDR, buf, 1);	//  K00-K07 0：OUT	1：IN
		if(ret == FAIL)
		{
			ret	= 12;
			break;	
		}
			
		buf[0] = 0XFF;
		ret = i2cWriteReg(AIP5916_ADDR, K1CONADDR, buf, 1);	//	K10-K17 0：OUT	1：IN 
		if(ret == FAIL)
		{
			ret	= 13;
			break;
		}
		
	#endif
		
		buf[0] = 0X04;
		ret = i2cWriteReg(AIP5916_ADDR, GSRADDR, buf, 1);		// 0X02-0X0F:赋值越大，灵敏度越高，对应扫描周期加长   9：300ms    6:200ms
		if(ret == FAIL)
		{
			ret	= 14;
			break;
		}

		// 灵敏度3
		for(i=0;i<sizeof(gtAipCfg)/sizeof(gtAipCfg[0]);i++)
		{
			buf[0] = gtAipCfg[i].gKorVal;
			//AIPDBG("aip5916 WR K0R%d,Val=%d \n",gtAipCfg[i].gKorAddr,buf[0]);		
			ret = i2cWriteReg(AIP5916_ADDR, gtAipCfg[i].gKorAddr, buf, 1);		// 阈值越大，按键灵敏度低，256级 4
			if(ret == FAIL)
			{
				ret	= 15;
				break;	
			}		
		}
		
			
		buf[0] = 0X00;
		ret = i2cWriteReg(AIP5916_ADDR, SYSCONADDR, buf, 1);		// 0X00：进入工作模式 		 
		if(ret == FAIL)
		{
			ret	= 16;
			break;
		}
	}while(0);
	if(ret)
	{
		AIPERR("aip5916ERR=%d \n",ret);	
		return false;
	}
	return true;
}


void checkAipKey(void) 
{
	uint8_t regBuf[1];
	
	//CTRL2 default 0x01 
	if(i2cReadReg(AIP5916_ADDR,KDR1ADDR,regBuf,1) == FAIL)
	{
		AIPERR("checkaipKeyERR\n");
		return;		//错误
	}
	AIPDBG("K1CON:%x\n",regBuf[0]);
	if(regBuf[0] != AIP_KDR1_VAL)	//未初始化
	{
		AIPERR("aipKeyReinit\n");
		aip59xxInit();
	}
}


uint8_t readAip59xxValue(uint8_t *pBuf)
{	
	uint8_t ret;
	
	ret= i2cReadReg(AIP5916_ADDR, KVR0ADDR, pBuf, 1);
	if(ret == FAIL)
		return ret;
	
	ret= i2cReadReg(AIP5916_ADDR, KVR1ADDR, pBuf+1, 1);
	return ret;
}








